<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="schoolId"
        placeholder="请选择所属院校"
        @change="selectChange()"
      >
        <el-option
          v-for="it in schoolList"
          :key="it.id"
          :label="it.name"
          :value="it.id"
        ></el-option>
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        size="small"
        icon="el-icon-circle-plus"
        @click="$router.push(add_url)"
        >添加</el-button
      >
    </div>
    <el-table
      :data="tableData"
      v-loading="loadList"
      element-loading-text="拼命加载中..."
      border
    >
      <el-table-column prop="url" label="链接"></el-table-column>
      <el-table-column prop="schoolId" label="院校">
        <template slot-scope="scope">
          <span>{{ findSchool(scope.row.schoolId).name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="image" label="图片">
        <template slot-scope="scope">
            <img :src="scope.row.image" style="width: 50px; height: auto" />
        </template>
      </el-table-column>

      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="edit(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger" @click="del(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- <div v-show="!loadList" class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 50, 100, 200]"
        :page-size="pageSize"
        layout="total,prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div> -->
  </div>
</template>

<script>
import { getList, del, getSchoolList } from "./api/index";
import { filterSchool } from "../../utils/common";
export default {
  data() {
    return {
      schoolList: [],
      schoolId: window.sessionStorage.getItem("schoolId")
        ? window.sessionStorage.getItem("schoolId") * 1
        : "",
      loadList: true,
      tableData: [],
      // getListFunc: Service.getList,
      // delItemFunc: Service.delItem,
      add_url: "/banner/add",
      edit_url: (id) => {
        return `/banner/${id}/edit`;
      },
    };
  },
  computed: {},
  created() {
    this.refresh_data();
    this.selectChange();
  },
  methods: {
    findSchool(id) {
      console.log(id, "----");
      const obj = this.schoolList.find((i) => i.id === id);
      return obj ? obj : { name: "" };
    },
    // 查询
    async selectChange() {
      if (!this.schoolId) return;
      window.sessionStorage.setItem("schoolId", this.schoolId);
      this.loadList = true;
      const res = await getList(this.schoolId);
      if (res.code === 1) {
        this.tableData = res.data;
      }
      this.loadList = false;
    },
    // 获取院校列表
    refresh_data() {
      getSchoolList()
        .then((data) => {
          console.log(data, "=====");
          if (data.code === 1) {
            this.schoolList = filterSchool(data.data);
          } else {
            this.$message.error(data.msg);
          }
          this.loadList = false;
        })
        .catch((err) => {
          console.log(err);
          this.loadList = false;
        });
    },
    edit(id) {
      this.$router.push(this.edit_url(id));
    },
    async del(item) {
      this.$confirm("确认删除该条数据？", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        this.loadList = true;
        const res = await del(item.id);
        if (res.code === 1) {
          this.selectChange();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>
<style scoped>
</style>
